<template>
  <div style="text-align: center;">
    <h2>你好！{{ userInfo.username }} </h2>
    <el-form :model="userInfo" :rules="rules" ref="userCenterForm" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="userInfo.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="userInfo.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('userCenterForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "UserCenter",
  data() {
    return {
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      }
    }
  },
  computed: {
    userInfo() {
      return this.$store.state.userinfo.user;
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.$axios.post('/sys/user/update', this.userInfo)
              .then(res => {
                this.$message({showClose: true, message: '恭喜，操作成功', type: 'success'});
              })
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
  .el-form {
    width: 420px;
    margin: 50px auto;
  }
</style>